package com.zhuiyi.zhxy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhuiyi.zhxy.pojo.Clazz;
import com.zhuiyi.zhxy.service.ClazzService;
import com.zhuiyi.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * @Author: zhuiyi
 * @CreatTime: 2023/1/22 21:05
 **/
@Api(tags = "班级控制器")
@RestController
@RequestMapping("/sms/clazzController")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    @ApiOperation("获取所有班级getClazzs")
    @GetMapping("/getClazzs")
    public Result getClazzs(){
        List<Clazz> clazzes = clazzService.getClazzs();
        return Result.ok(clazzes);
    }

    @ApiOperation("删除Clazz")
    @DeleteMapping("/deleteClazz")
    public Result deleteClazz(@RequestBody List<Integer> ids){
        clazzService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("新增或修改Clazz")
    @PostMapping("/saveOrUpdateClazz")
    public Result saveOrUpdateClazz(
            @RequestBody Clazz clazz
    ){
        if (clazzService.saveOrUpdate(clazz)) {
            return Result.ok();
        }
        return Result.fail();
    }

    //sms/clazzController/getClazzsByOpr/1/3?name
    @ApiOperation("Clazz班级分页查询")
    @GetMapping("/getClazzsByOpr/{pageNo}/{pageSize}")
    public Result getClazzsByOpr(
            @ApiParam("当前多少页") @PathVariable("pageNo") Integer pageNo,
            @ApiParam("每页多少条记录") @PathVariable("pageSize") Integer pageSize,
//            @ApiParam("分页查询条件，年级名称") String gradeName,
//            @ApiParam("分页查询条件，班级名称") String name
            @ApiParam("分页查询条件，年级名称gradeName，和班级名称name封装为一个对象") Clazz clazz
    ){
        //处理参数
        Page<Clazz> page =new Page<Clazz>(pageNo,pageSize);

        //调用service层
        IPage<Clazz> iPage  = clazzService.getClazzByOpr(page,clazz);

        return Result.ok(iPage);
    }
}
